//
//  vdButton.m
//  Void Dead
//
//  Created by Sidney Just on 08.12.09.
//
//  Copyright © 2009 by Sidney Just
//  Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
//  documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
//  the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, 
//  and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//  The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, 
//  INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR 
//  PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE 
//  FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 
//  ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

#import "vdButton.h"


@implementation vdButton
@synthesize enabled;

#pragma mark -
#pragma mark Touches

- (void)getTouch:(CGPoint)point {
	if(enabled)
	{
		if((point.x >= aPos_x && point.x <= aPos_x + size_x) && (point.y >= aPos_y && point.y <= aPos_y + size_y))
		{
			if(statePressed)
			{
				[self setTexture:statePressed];
				
				pressed = YES;
			}
			
		} else {
			if(stateNormal)
			{
				[self setTexture:stateNormal];
				pressed = NO;
			}
		}
	} else {
		[self setTexture:stateDisabled];
	}
}

- (void)endSequence:(CGPoint)point {
	if(enabled)
	{
		if(pressed && pressedDelegate)
		{
			if([pressedDelegate conformsToProtocol:@protocol(vdInterfaceProtocol)])
			{
				if([pressedDelegate respondsToSelector:@selector(buttonTouchUpInside:)])
					[pressedDelegate buttonTouchUpInside:self];
			}
		}
		
		if(stateNormal)
		{
			[self setTexture:stateNormal];
			pressed = NO;
		}
	} else {
		[self setTexture:stateDisabled];
	}
}

#pragma mark -
#pragma mark States

- (void)setNormalState:(vdTexNode *)tex {
	stateNormal = tex;
	if(!pressed)
	{
		[self setTexture:stateNormal];
	}
}

- (void)setPressedState:(vdTexNode *)tex {
	statePressed = tex;
	
	if(pressed)
	{
		[self setTexture:statePressed];
	}
}

- (void)setDisabledState:(vdTexNode *)tex {
	stateDisabled = tex;
	
	if(!enabled)
	{
		[self setTexture:stateDisabled];
	}
}

- (void)setDelegate:(id)del {
	pressedDelegate = del;
}

#pragma mark -

- (id)initWithNormalState:(NSString *)nstate andPressedState:(NSString *)pstate {
	if(self = [super initWithTexture:NULL])
	{
		stateNormal = NULL;
		statePressed = NULL;
		stateDisabled = NULL;
		
		pressedDelegate = NULL;
		
		pressed = NO;
		
		enabled = YES;
		
		[self setNormalState:[[vdTexManager sharedManager] loadTexture:nstate]];
		[self setPressedState:[[vdTexManager sharedManager] loadTexture:pstate]];
	}
	return self;
}

- (id)init {
	if(self = [super initWithTexture:NULL])
	{
		stateNormal = NULL;
		statePressed = NULL;
		stateDisabled = NULL;
		
		pressedDelegate = NULL;
		
		pressed = NO;
		
		enabled = YES;
	}
	return self;
}

- (void)dealloc {
	[self cleanChildren];
	
	[super dealloc];
}

@end
